<template>
  <el-dialog :visible.sync="showDialog" :close-on-click-modal="modalDialog" :close-on-press-escape="modalDialog" width="800px" :title="title" @close="closeDialog">
    <el-form ref="qdForm" :model="qdForm" label-width="auto" :rules="rules" label-suffix="：" @submit.native.prevent>
      <el-form-item :label="$t('message.yhxx')" prop="name">
        <MemberSearch ref="memberSearchRefa" @clear="MemberClear" @select="MemberSelect" />
      </el-form-item>
      <div v-if="TicketAbleResult" class="verificat_info_wrap">
        <h2>{{ $t('message.hxjl') }}：</h2>
        <div class="verificat_info-main">
          <div class="left">
            <div v-if="TicketAbleList.length == 0" class="default_noticket">
              <img src="@/assets/images/default_noticket.png" alt="">
            </div>
            <template v-if="TicketAbleList.length > 0">
              <div v-for="item in TicketAbleList" :key="item.id" class="info_item" :class="{ active: item.id === activeTicketItem.id }" @click="ticketItemClick(item)">
                <h2>{{ item.name }}</h2>
                <div class="line">
                  <span v-if="item.useTime " class="span6">{{ $t('message.zksysj') }}：<span class="primaryColor">{{
                    item.useTime
                  }}</span></span>
                </div>
                <div class="line">
                  <span class="span4">{{ $t('message.sycs2') }}：{{ item.num || '-' }}</span>
                </div>

              </div>
            </template>
          </div>
          <div v-if="verificatInfo.id" class="right">
            <div class="head_wrap">
              <el-image shape="circle" style="width: 40px; height: 40px; border-radius: 50%" fit="cover" class="flex-cc mr10" :src="
                  verificatInfo.showAvatar
                    ? verificatInfo.showAvatar
                    : default_pic
                " :preview-src-list="[
                  verificatInfo.showAvatar
                    ? verificatInfo.showAvatar
                    : default_pic,
                ]">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture" />
                </div>
              </el-image>
              <div class="wrap">
                <div class="name">{{ verificatInfo.nickName }}</div>
                <div class="phone">{{ verificatInfo.phone }}</div>
              </div>
            </div>
            <div v-if="verificatInfo.realName" class="line">
              <span class="key">{{ $t('message.zsxm') }}：</span>
              {{ verificatInfo.realName }}
            </div>
            <div class="line">
              <span class="key">{{ $t('message.sex') }}：</span>{{
                verificatInfo.sex === "1"
                  ? $t('message.nan')
                  : verificatInfo.sex === "2"
                    ? $t('message.nv')
                    : $t('message.bm')
              }}
            </div>
            <div v-if="verificatInfo.identityNumber" class="line">
              <span class="key">{{ $t('message.sfzh') }}：</span>{{ verificatInfo.identityNumber || "" }}
            </div>
            <div v-if="verificatInfo.number" class="line">
              <span class="key">{{ $t('message.hykh') }}：</span>{{ verificatInfo.number }}
            </div>
            <div v-if="verificatInfo.rechargeMoney" class="line">
              <span class="key">{{ $t('message.czje') }}：</span>{{ verificatInfo.rechargeMoney }}
            </div>
            <div v-if="verificatInfo.giveMoney" class="line">
              <span class="key">{{ $t('message.zsye') }}：</span>{{ verificatInfo.giveMoney }}
            </div>
            <div v-if="verificatInfo.integral" class="line">
              <span class="key">{{ $t('message.sfye') }}：</span>{{ verificatInfo.integral }}
            </div>
          </div>
        </div>
      </div>
    </el-form>
    <div slot="footer" class="verificat_footer">
      <div />
      <div>
        <el-button @click="closeDialog">{{ $t('message.qx') }}</el-button>
        <el-button type="primary" :loading="qdLoading" @click="qdbtn">{{ $t('message.qrhx') }}</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import {
  escortQueryRecord,
  escortRecordVerify
} from '@/api'
import store from '@/store'
import MemberSearch from '@/components/MemberSearch'
export default {
  components: {
    MemberSearch
  },
  data () {
    return {
      title: this.$t('message.bykhx'),
      modalDialog: store.getters.dialog,
      showDialog: false,
      loading: false,
      qdLoading: false,
      memberId: '',
      tgMemberInfo: {},
      qdForm: {},
      TicketAbleList: [],
      TicketAbleResult: false,
      activeTicketItem: {},
      verificatInfo: {},
      default_pic: require('@/icons/png/default_pic.png'),
      rules: {
        escortId: [
          { required: true, message: this.$t('message.qxz') + this.$t('message.byk'), trigger: 'change' }
        ],
        escortMemberId: [
          { required: true, message: this.$t('message.qxz') + this.$t('message.gkhy'), trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    open () {
      this.showDialog = true
      this.qdForm = {}
      this.memberId = ''
      this.verificatInfo = {}
      this.TicketAbleList = []
      this.TicketAbleResult = false
      this.activeTicketItem = {}
    },
    MemberSelect (item) {
      const that = this
      const fn = () => {
        that.memberId = item.id
        that.verificatInfo = item
        that.getList()
      }
      fn()
    },
    MemberClear () {
      this.verificatInfo = {}
      this.memberId = ''
    },
    closeDialog () {
      this.showDialog = false
      this.qdForm = {}
      this.$emit('close')
    },
    ticketItemClick (item) {
      this.activeTicketItem = { ...item }
    },
    getList () {
      this.TicketAbleList = []
      this.TicketAbleResult = false
      this.activeTicketItem = {}
      escortQueryRecord({ param: { memberId: this.memberId } }).then((resb) => {
        if (resb.errorCode !== '000000') return
        this.TicketAbleResult = true
        this.TicketAbleList = resb.recordList
        if (resb.recordList.length > 0) {
          this.activeTicketItem = resb.recordList[0]
        }
      })
    },
    qdbtn () {
      let param = {}
      if (!(this.TicketAbleResult && this.activeTicketItem.id)) {
        return this.$message.error(this.$t('message.wjcdxyxx'))
      }
      this.$refs.qdForm.validate((valid) => {
        if (valid) {
          this.qdLoading = true
          param = { memberId: this.memberId, recordId: this.activeTicketItem.id }
          escortRecordVerify({ param }).then((resb) => {
            this.qdLoading = false
            if (resb.errorCode !== '000000') return
            this.TicketAbleResult = false
            this.$message.success(this.$t('message.hxcg'))
            this.showDialog = false
            this.$emit('close')
          }).catch(() => {
            this.qdLoading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";
.verificat_info_wrap {
  margin-bottom: 20px;
  .verificat_info-main {
    display: flex;

    .left {
      flex: 1;
      overflow: auto;
      height: 380px;
      margin-right: 20px;

      .info_item {
        cursor: pointer;
        padding: 6px 12px;
        margin-bottom: 10px;
        border-radius: 2px;
        border: 1px solid #dfe2e8;

        h2 {
          padding: 5px 0;
          margin: 0;
          font-size: 16px;
        }

        .line {
          display: flex;
          padding: 5px 0;

          .span6 {
            flex: 5;
          }

          .span4 {
            flex: 5;
          }
        }
      }

      .active {
        border: 1px solid #435ebe;
        background: #f4f7fc;
      }

      .default_noticket {
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;

        img {
          width: 160px;
        }
      }
    }

    .right {
      width: 280px;
      height: 380px;
      background-color: #f4f7fc;
      padding: 20px;

      .head_wrap {
        display: flex;
        align-items: center;
        margin-bottom: 30px;

        .wrap {
          flex: 1;

          .name {
            font-size: 14px;
            font-weight: 600;
            margin-bottom: 10px;
          }

          .phone {
            color: #666666;
          }
        }
      }

      .line {
        margin-top: 20px;

        .key {
          display: inline-block;
          // width: 70px;
        }
      }
    }
  }
}

.verificat_footer {
  text-align: left;
  display: flex;
  justify-content: space-between;
  .verificat_num_wrap {
    flex: 1;
  }
}
</style>
